package msb.dongbao.geverator.study;

public class ForDemo {
    public static void main(String[] args){
//        100以内奇数的和
        ForDemo forDemo = new ForDemo();
        int evenSum = forDemo.sum(100,1);
        System.out.println("100以内奇数的和："+evenSum);
        int oddSum = forDemo.sum(100,2);
        System.out.println("100以内偶数的和："+oddSum);
        int allSum = forDemo.sum(100,0);
        System.out.println("100以内整数的和："+allSum);
// 1-1000 之间能被5整除的数 且每行输出3个
        int[] divisorArray = forDemo.exactDivision(1000,5);
        System.out.println("1-1000 之间能被5整除的数有"+divisorArray.length+"个");
//        将10进制 整数 变成 二进制
//        求 1！+2！+10！
        int factorial = forDemo.factorial(10);
        System.out.println(10+"的阶乘是："+factorial);
    }
    public int factorial(int n){
        if(n==0){
            return 1;
        }else if(n<0){
            throw new IllegalArgumentException("必须为正整数!");
        }
        int factorial = 1;
        for(int i = 1;i <= n;i++){
            factorial *= i;
        }
        return factorial;

    }
    public int[] exactDivision(int limit,int divisor){
        int[] divisorArray = new int[limit/divisor];
        for(int i=0,j=1;j<=limit;j++){
            if(j % divisor == 0){
//                System.out.println("第"+i+"个数"+j);
                System.out.print("第"+(i+1)+":"+ j + "  ");
                if(i%3==0&&i>0){
                    System.out.println();
                }
                divisorArray[i] = j;
                i++;
            }

        }
        return divisorArray;
    }

    /**
     *
     * @param limit
     * @param type
     * @return
     */
    public int sum(int limit,int type){
        if(type==1){
            return this.evenSum(limit);
        }else if(type==2){
            return this.oddSum(limit);
        }else {
            return this.allSum(limit);
        }
    }

    /**
     *
     * @param limit
     * @return
     */
    private int allSum(int limit){
        int allSum = 0;
        for(int start=1;start<=limit;start++){
            allSum += start;
        }
        return allSum;
    }

    /**
     *
     * @param limit
     * @return
     */
    private int oddSum(int limit){
        int oddSum = 0;
        for(int start=1;start<=limit;start++){
            if(start%2==0){
                oddSum += start;
            }
        }
        return oddSum;
    }

    /**
     *
     * @param limit
     * @return
     */
    private int evenSum(int limit){
        int evenSum = 0;
        for(int start=1;start<=limit;start++){
            if(start%2!=0){
                evenSum += start;
            }
        }
        return evenSum;
    }
}

